package controller.impl;

import controller.GetWebMsgInterface;
import encryption.BasicUtil;
import encryption.MD5Util;
import encryption.RSA;
import model.Yangzhou;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class GetWebMsg implements GetWebMsgInterface {

    private static final Integer CONNECT_TIMEOUT = 1000;

    public String getJson2StringMsg(Yangzhou yangzhou) {

        String MERACCOUNT = "321000000";
        String VER = "3.0";

        JSONObject original = new JSONObject();
        original.put("meraccount", MERACCOUNT);
        original.put("scode", yangzhou.getScode());
        original.put("date", yangzhou.getDate());
        original.put("val", yangzhou.getVal());
        original.put("flag", yangzhou.getFlag());
        original.put("method", "queryJsd");
        original.put("timestamp", System.currentTimeMillis());
        original.put("v", VER);

        String param_key = getparamkey(original);
        String secret_key = getsecretkey(original);
        System.out.println(param_key);
        System.out.println(secret_key);

        String getJson = null;
        if (param_key != null && secret_key != null) {
            String url = null;
            Boolean isRegular = false;
            if (yangzhou.getScode().length() == 0) {
                url = "http://192.168.200.207:8088/lsjsdService/3.0/hts/queryJsd?param_key=" + param_key + "&secret_key=" + secret_key;
                isRegular = true;
            } else {
                url = "http://192.168.200.207:8088/lanshuijsdService/3.0/hts/queryJsd?param_key=" + param_key + "&secret_key=" + secret_key;
            }
            getJson = loadJSON(url, isRegular);
        }

        if (getJson != null && !("").equals(getJson)) {
            return getJson;
        } else {
            return null;
        }
    }

    public String getparamkey(JSONObject jsonObject) {
        try {
            String PUBLICKEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCf2BEB3CIz0fojBPc7uNecFu0XTjGXdcBGv+tfGqSO3QM8tg4h+/geFLU/JjABJjb7G9w40lDqz0KYK0xtmx7KA7SPjuObcvK36c0/lqC0vlpUOiJ85eTYq7pZJbugulkMXAjLRbENGphfE0kREgOknOEcIlMCuDAwEo6h93WyIwIDAQAB";
            return RSA.encrypt(URLEncoder.encode(jsonObject.toString(), "UTF-8"), PUBLICKEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getsecretkey(JSONObject jsonObject) {
        try {
            return URLEncoder.encode(MD5Util.MD5(BasicUtil.paramSort(jsonObject)), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String loadJSON(String url, Boolean isRegular) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            if (!isRegular) {
                yc.setConnectTimeout(CONNECT_TIMEOUT);
                yc.setReadTimeout(CONNECT_TIMEOUT);
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException ignored) {
        } catch (IOException ignored) {
        }
        return json.toString();
    }
}
